/**
 * @Date: Apr 15, 2010 8:02:37 PM
 */
package com.philip.core;

import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

import com.philip.journal.core.service.ServiceFacade;

/**
 * Super class for Service Proxy instance.
 *
 * @author cry30
 */
public abstract class BusinessServiceProxy { // NOPMD by r39 on 3/30/11 9:14 AM

    /** Subclass-ready logger. */
    private Logger logger = Logger.getLogger(getClass()); // NOPMD by r39 on 3/30/11 9:13 AM

    /** Session key for the facade instance. */
    public static final String SERVICE_FACADE = "ApplicationFacade";

    /** Mapping of session ID to Business Session instances. */
    private static Map<String, Map<String, Object>> sessionMap;
    static {
        sessionMap = new HashMap<String, Map<String, Object>>();
    }

    /** Reference for application specific facade instance. */
    private transient BusinessServiceFacade serviceFacade;

    /** Reference to current session id. */
    private transient String sessionId;

    /**
     * Proxy method call to application specific business facade.
     *
     * @param paramType
     *            Array of parameter types passed to the proxy method.
     * @param args
     *            arguments passed to the proxy method.
     * @return any value returned by the proxy call to application facade.
     * @throws Exception
     *             this is a reflection call so expect all kinds of exception thrown here.
     */
    protected Object proxyMehodCall(final Class<?>[] paramType, final Object[] args) throws Exception {//NOPMD internal framework code.
        if (sessionMap.get(sessionId) == null) {
            final Map<String, Object> userSessionMap = new HashMap<String, Object>();
            userSessionMap.put(SERVICE_FACADE, serviceFacade);
            sessionMap.put(sessionId, userSessionMap);
        }
        final Map<String, Object> userSession = sessionMap.get(sessionId);
        return ServiceProxyHelper.proxyMehodCall(serviceFacade, userSession, paramType, args);
    }

    /**
     * Accessor method.
     *
     * @param pSessionId
     *            session id to set.
     */
    public final void setSessionId(final String pSessionId) {
        this.sessionId = pSessionId;
    }

    /**
     * Accessor method.
     *
     * @param pServiceFacade
     *            ServiceFacade instance to set.
     */
    public final void setServiceFacade(final ServiceFacade pServiceFacade) {
        this.serviceFacade = pServiceFacade;
    }

    /**
     * Accessor method.
     *
     * @return the logger
     */
    public final Logger getLogger() {
        return logger;
    }

    /**
     * Accessor method.
     *
     * @param plogger
     *            the logger to set
     */
    public final void setLogger(final Logger plogger) {
        this.logger = plogger;
    }
}
